/*  File Name	: GateMonitor.h
 *  Author		: Javie Pong
 *	 E-mail		: 823960168@qq.com
 *  Created on	: 2012-7-1
 *  Description:
 */

#ifndef GATEMONITOR_H_
#define GATEMONITOR_H_

#include "MonitorSock.h"
#include "GateService.h"
#include "Acceptor.h"
#include "SockAcceptor.h"
#include "GatePlayer.h"
#include "Configurator.h"

class RoomInfo
{
	friend class GateMonitor;
public:
	RoomInfo(int p=0);
	virtual ~RoomInfo();

	void set(int type,int room_id,std::string ip,int port,
			int limit_member,int min_chips,int max_chips,GateService *es);
	int cur_member(void);
	int limit_member(void);
	int add_member(void);
	int sub_member(void);

private:
	int gametype;
	int room_id;//房间id
	int member;//当前房间人数
	int member_limit;//房间人数上限
	int min_chips;//最小筹码
	int max_chips;//最大筹码
	int port;
	bool is_regsiter;
	GateService *es;
	std::string ip;
};

class GateMonitor:virtual public Monitor<GateService>
{
public:
	typedef MonitorAcceptor<GateMonitor,GateService> GateAcceptor;
	typedef MonitorConnector<GateMonitor,GateService> GateConnector;
	typedef ObjectPool<GatePlayer>	GatePlayerPool;
	typedef std::map<int,GatePlayer*> GatePlayerMap;
	typedef std::map<int,RoomInfo*> EngineMap;

	struct RoutineTimer:public Timer
	{
		RoutineTimer()
		{
			monitor = NULL;
		}
		virtual int handle_timeout(void);
		GateMonitor *monitor;
	};

private:
	typedef Monitor<GateService> super;

public:
	virtual ~GateMonitor();

	static GateMonitor *instance(void);

	virtual const int monitor_id(void);

	virtual int init(int line_id,int list_size=10240,int block_size=10240,int pool_size=10240,
			int growth_size=1024,std::string pool_name="GateMonitor-Msg");

	virtual int start();

	virtual int stop(void);

	virtual int run_request_loop(void);
	virtual int run_respond_loop(void);

	static int run_account_chk(void *p);
	int run_account_chk(void);

	virtual void collection_info(void);

	int create_account(int svc_id,MessageBlock *mb);

	int log_out(int roleid,MessageBlock *mb);

	int enter_room(int roleid,MessageBlock *mb,bool inner=false);
	int exit_room(int roleid,MessageBlock *mb=NULL);
	int switch_room(int roleid,MessageBlock *mb,bool inner=false);

	int recharge(int svc_id,MessageBlock *mb);

	int respond_succ_to_client(int id,int cmd,MessageBlock *mb=NULL);
	int respond_error_to_client(int id,int cmd,int error,MessageBlock *mb=NULL);

	GatePlayerPool *player_pool(void);

	const int gate_id(void);

	const Json::Value &cfg(void);

	int log_out(GateService *gs);

	GatePlayer *find_player(int roleid);
	int bind_player(int role_id,GatePlayer *p);
	int unbind_player(int role_id);

	RoomInfo *find_room(int room_id);
	int bind_room(int room_id,RoomInfo *room);
	int unbind_room(int room_id);

	int disconn_dircserver(void);
	int disconn_dbserver(void);

	int send_room_info(bool all,int roomid=0);
private:
	GateMonitor();

	int dispatch_request(int room_id,MessageBlock *mb);
	int process_request(MessageBlock *mb);
	int process_respond(MessageBlock *mb);
	int process_engine_request(int cmd,int roleid,MessageBlock *mb);
	int process_send_out(int cmd,int roleid,MessageBlock *mb);

	//dbserver
	int conn_dbserver(void);
	int log_in(int svc_id,MessageBlock *mb);
	int process_account_chk(MessageBlock *mb);
	int send_account_chk(int svc,MessageBlock *mb);
	int recv_account_chk(int svc,MessageBlock *mb);
	int send_to_dbserver(MessageBlock *mb);

	int inner_error_info(int roleid,MessageBlock *mb);

	int engine_register(GateService *gs,MessageBlock *mb);
	int register_room_return(MessageBlock *mb);
	int make_up_room_info(MessageBlock *mb);

	int conn_dircserver(void);
	int register_to_dirc(void);

	static GateMonitor *instance_;

	GateAcceptor *acceptor_;
	GateConnector *connector_;

	GatePlayerPool player_pool_;
	GatePlayerMap player_map_;
	EngineMap engine_map_;

	bool register_dirc_;//注册目录服务是否成功
	bool room_change_;
	GateService *dircserver_;
	RoutineTimer routine_timer_;

	int game_type_;
	int gate_id_;
	int line_id_;
	int player_count_;//当前连接总数

	RT_MUTEX rt_mutex_;

	DuplexList *chk_account_list_;
	GateService *dbserver_;
};

#endif /* GATEMONITOR_H_ */
